///<reference path="../_me/me.d.ts"/>
///<reference path="moment.d.ts"/>
var Site_Feed_App;
(function (Site_Feed_App) {
    var app = (function () {
        function app() {
        }
        app.prototype.Render = function (scope, output) {
            var _this = this;
            if (MobileEntree.Utility.IsNullOrEmpty(this.SPHostUrl)) {
                //set this on initial load to use for loading and posting to the site feed, ME framework adds window.QueryString object with all available keys
                this.SPHostUrl = window["QueryString"]["SPHostUrl"];
            }
            if (scope.Properties["Type"] && scope.Properties["Type"] == "ShowNewPostForm") {
                return this.renderNewPostForm(output[0], scope.Properties["ThreadId"]);
            } else if (scope.Properties["Type"] && scope.Properties["Type"] == "CreateNewPost") {
                return this.postToSiteFeed(scope.Properties["FormData"]["PostText"], scope.Properties["ThreadId"]).then(function () {
                    //render feed or thread depending on if this was a new post or a reply
                    if (MobileEntree.Utility.IsNullOrEmpty(scope.Properties["ThreadId"])) {
                        return _this.renderFeed(output[0]);
                    } else {
                        return _this.renderThread(output[0], scope);
                    }
                });
            } else if (scope.Properties["Type"] && scope.Properties["Type"] == "RenderThread") {
                return this.renderThread(output[0], scope);
            } else {
                return this.renderFeed(output[0]);
            }
        };

        app.prototype.renderFeed = function (output) {
            var _this = this;
            var that = this;
            return $.Deferred(function (deferred) {
                output.PanelIndex = 0; //ensure the feed renders in the left panel even after posting.
                output.Title = "Site Feed";
                _this.getSiteFeed().then(function (threads) {
                    //add home button
                    var homeButton = new MobileEntree.ObjectModel.ActionItem_Home();

                    //empty ClickAction to just refresh this screen
                    homeButton.ClickAction = new MobileEntree.ObjectModel.ClickAction();
                    homeButton.ClickAction.AnimationStyle = MobileEntree.Enumerations.AnimationStyles.None;
                    output.HomeBar.ActionItems.push(homeButton);
                    output.ActionTray.ActionLanes.push(_this.createNewPostActionLane());
                    var group = new MobileEntree.ObjectModel.ListGroup("Site Feed");
                    $.each(threads, function (index, thread) {
                        group.ListItems.push(that.createSocialPostListItem(thread, thread.get_rootPost(), true));
                    });
                    output.Data.ListGroups.push(group);
                    deferred.resolve();
                }, function () {
                    deferred.reject();
                });
            }).promise();
        };

        app.prototype.renderThread = function (output, scope) {
            var that = this;
            var threadId = scope.Properties["ThreadId"];
            return $.Deferred(function (deferred) {
                output.Title = "Social Thread";
                that.getThread(threadId).then(function (thread) {
                    var rootGroup = new MobileEntree.ObjectModel.ListGroup("Root Post");
                    rootGroup.ListItems.push(that.createSocialPostListItem(thread, thread.get_rootPost()));
                    output.Data.ListGroups.push(rootGroup);
                    var replyGroup = new MobileEntree.ObjectModel.ListGroup("Replies");
                    replyGroup.Expandable = true;
                    $.each(thread.get_replies(), function (index, post) {
                        replyGroup.ListItems.push(that.createSocialPostListItem(thread, post));
                    });
                    if (replyGroup.ListItems.length == 0) {
                        replyGroup.ListItems.push(new MobileEntree.ObjectModel.ListItem_Billboard("<p>No Replies</p>"));
                    }
                    output.Data.ListGroups.push(replyGroup);
                    output.ActionTray.ActionLanes.push(that.createNewPostActionLane(threadId));

                    //if in single panel mode add a Back button
                    if (scope.PanelCount < 2) {
                        var backButton = new MobileEntree.ObjectModel.ActionItem_Back();

                        //use empty clickaction to render the default screen
                        backButton.ClickAction = new MobileEntree.ObjectModel.ClickAction();
                        backButton.ClickAction.AnimationStyle = MobileEntree.Enumerations.AnimationStyles.SlideRight;

                        //add to HomeBar.ActionItems
                        output.HomeBar.ActionItems.push(backButton);
                    }
                    deferred.resolve();
                });
            }).promise();
        };

        app.prototype.renderNewPostForm = function (output, threadId) {
            var that = this;
            return $.Deferred(function (deferred) {
                var group = new MobileEntree.ObjectModel.ListGroup("Post Form");
                var postText = new MobileEntree.ObjectModel.FormItem_Note("PostText", "Post Text", "", 6);
                group.ListItems.push(postText);
                output.Data.ListGroups.push(group);
                var actionItem = new MobileEntree.ObjectModel.ActionItem("socialfeedapp", "Post", 0);
                actionItem.ClickAction = new MobileEntree.ObjectModel.ClickAction({ Type: "CreateNewPost" });
                actionItem.ClickAction.ParseFormData = true;
                actionItem.ClickAction.AnimationStyle = MobileEntree.Enumerations.AnimationStyles.DropFlipYBack;
                if (!MobileEntree.Utility.IsNullOrEmpty(threadId)) {
                    actionItem.ClickAction.Properties["ThreadId"] = threadId;
                }
                var actionLane = new MobileEntree.ObjectModel.ActionLane("socialfeedapp", new Array(actionItem), 0);
                output.ActionTray.ActionLanes.push(actionLane);
                output.Data.ItemStyle = MobileEntree.Enumerations.ListViewStyles.ContextMenu;
                deferred.resolve();
            }).promise();
        };

        app.prototype.createSocialPostListItem = function (thread, post, isIndexView) {
            var item = new MobileEntree.ObjectModel.ListItem_TitleCaption(post.get_text());

            var author = thread.get_actors()[post.get_authorIndex()].get_name();

            //use moment library to get relative date
            var caption = "Posted by " + author + " " + moment(post.get_createdTime()).fromNow();
            if (isIndexView) {
                //add reply count and clickAction if in index view
                caption += " with " + thread.get_totalReplyCount() + " replies.";
                item.ClickAction = new MobileEntree.ObjectModel.ClickAction({ ThreadId: thread.get_id(), Type: "RenderThread" });
            }
            item.Caption = caption;
            return item;
        };

        app.prototype.createNewPostActionLane = function (threadId) {
            //create actionLane/Item and set properties depending on threadId
            var actionItemText = !MobileEntree.Utility.IsNullOrEmpty(threadId) ? "Reply" : "New Post";
            var actionItem = new MobileEntree.ObjectModel.ActionItem("socialfeedapp", actionItemText);
            actionItem.ClickAction = new MobileEntree.ObjectModel.ClickAction({ Type: "ShowNewPostForm" });

            //set properties to make this clickAction open the Context Menu
            actionItem.ClickAction.AnimationStyle = MobileEntree.Enumerations.AnimationStyles.DropFlipYForward;
            actionItem.ClickAction.TargetType = MobileEntree.Enumerations.ClickActionTargetType.ContextMenu;
            if (!MobileEntree.Utility.IsNullOrEmpty(threadId)) {
                actionItem.ClickAction.Properties["ThreadId"] = threadId;
            }
            var lane = new MobileEntree.ObjectModel.ActionLane("socialfeedapp");
            lane.ActionItems.push(actionItem);
            return lane;
        };

        app.prototype.getSiteFeed = function () {
            var _this = this;
            var that = this;
            return $.Deferred(function (deferred) {
                var context = SP.ClientContext.get_current();
                var feedManager = new SP.Social.SocialFeedManager(context);
                var options = new SP.Social.SocialFeedOptions();
                options.set_maxThreadCount(10);
                var feed = feedManager.getFeedFor(_this.SPHostUrl, options);
                context.executeQueryAsync(function (sender, args) {
                    var threads = feed.get_threads();
                    deferred.resolve(feed.get_threads());
                }, function (sender, error) {
                    alert('Error getting feed: ' + error.get_message());
                    console.log(error);
                    deferred.reject();
                });
            }).promise();
        };
        app.prototype.getThread = function (threadId) {
            var that = this;
            return $.Deferred(function (deferred) {
                var context = SP.ClientContext.get_current();
                var feedManager = new SP.Social.SocialFeedManager(context);
                var thread = feedManager.getFullThread(threadId);
                context.executeQueryAsync(function (sender, args) {
                    deferred.resolve(thread);
                }, function (sender, args) {
                    alert('Error getting thread: ' + args.get_message());
                    console.log(args);
                    deferred.reject();
                });
            }).promise();
        };

        app.prototype.postToSiteFeed = function (message, threadId) {
            var _this = this;
            var that = this;
            return $.Deferred(function (deferred) {
                var context = SP.ClientContext.get_current();
                var feedManager = new SP.Social.SocialFeedManager(context);
                var creationData = new SP.Social.SocialPostCreationData();
                creationData.set_contentText(message);
                var appLink = new SP.Social.SocialLink();

                //add app link, text doesn't seem to work shows as Title of SP App
                appLink.set_text("Posted from Mobile Entree 365");
                appLink.set_uri("http://www.mobileentree.com");
                creationData.set_source(appLink);

                //set source to threadId if replying or SPHostUrl if creating a new post
                var source = MobileEntree.Utility.IsNullOrEmpty(threadId) ? _this.SPHostUrl : threadId;
                feedManager.createPost(source, creationData);
                context.executeQueryAsync(function (sender, args) {
                    deferred.resolve();
                }, function (sender, error) {
                    alert(error.get_message());
                    deferred.reject(error);
                });
            }).promise();
        };
        return app;
    })();
    Site_Feed_App.app = app;
})(Site_Feed_App || (Site_Feed_App = {}));
//@ sourceMappingURL=app.js.map
//# sourceMappingURL=app.js.map
